package com.live.dengdengtv.activity.live;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.live.dengdengtv.CustomTitleActivity;
import com.live.dengdengtv.R;
import com.live.dengdengtv.activity.my.MyAccountActivity;
import com.live.dengdengtv.activity.support.CurLiveInfo;
import com.live.dengdengtv.bean.AppInfo;
import com.live.dengdengtv.bean.LoginInfo;
import com.live.dengdengtv.bean.SimpleInfoData;
import com.live.dengdengtv.net.NetTaskList;
import com.live.dengdengtv.utils.Notice;

import java.util.HashMap;

public class RedpacketActivity extends CustomTitleActivity implements View.OnClickListener{

    public static final String Extra_MEMBERNUM = "memberNum";

    public static final String Extra_redCount = "redCount";
    public static final String Extra_virCoin = "virCoin";
    public static final String Extra_redpacketId = "redpacketId";

    private EditText msg_tv;
    private EditText redpacket_count_et;
    private EditText virCoin_et;

    private int redCount = 0;
    private int virCoin = 0;
    private String msg = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_redpacket,R.layout.title_custom_red);
        setDefaultTitle("发红包");

        initView();
    }

    private void initView() {

        Intent intent = getIntent();
        String memberNum = "0";
        String currentVircoin = "0";
        try {
            memberNum = intent.getStringExtra(Extra_MEMBERNUM);
            currentVircoin = LoginInfo.getInstance().getgUserInfo().virtual_coin;
        } catch (Exception e) {
            e.printStackTrace();
        }
        TextView memberNum_tv = (TextView) findViewById(R.id.memberNum_tv);
        TextView current_virCoin_tv = (TextView) findViewById(R.id.current_virCoin_tv);
        memberNum_tv.setText(memberNum);
        current_virCoin_tv.setText(currentVircoin);


        redpacket_count_et = (EditText) findViewById(R.id.redpacket_count_et);
        virCoin_et = (EditText) findViewById(R.id.virCoin_et);
        msg_tv = (EditText) findViewById(R.id.msg_tv);


        TextView recharge_tv = (TextView) findViewById(R.id.recharge_tv);
        Button send_bt = (Button) findViewById(R.id.send_bt);
        recharge_tv.setOnClickListener(this);
        send_bt.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.recharge_tv){
            startActivityForResult(new Intent(this, MyAccountActivity.class),100);
        }else if(v.getId() == R.id.send_bt){
            sendRedpacket();
        }

    }

    private void sendRedpacket(){

        try {
            redCount = Integer.parseInt(redpacket_count_et.getText().toString().trim());
            virCoin = Integer.parseInt(virCoin_et.getText().toString().trim());
        } catch (Exception e) {
            e.printStackTrace();
        }

        if(redCount <= 0){
          Notice.alert(context,"红包必须为整数且大于0!");
            return;
        }
        if(virCoin <= 0){
          Notice.alert(context,"虚拟币必须为整数且大于0!");
            return;
        }

        HashMap<String,String> params = new HashMap<String, String>();
        params.put("title",msg_tv.getText().toString().trim());//红包标题
        params.put("count_of_getters",redCount+"");//红包个数
        params.put("get_live_id", CurLiveInfo.getLiveId()+"");//直播ID
        params.put("count_of_virtual_coin",virCoin+"");//发送的虚拟币总数
        params.putAll(AppInfo.getHeadInfo());
        requestData(NetTaskList.send_redpacket, params,1);
    }


    @Override
    public boolean onResponse(SimpleInfoData response, int id) {
        if (id == 1) {
            if(response.result == 0){
                LoginInfo.getInstance().subtractVirCoin(virCoin+"");//刷新用户虚拟币
                String packetId = "";
                try {
                    packetId = response.data.getAsJsonObject().get("id").getAsString();//int
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Intent intent  = new Intent();
                intent.putExtra(Extra_redpacketId, packetId);
                intent.putExtra(Extra_redCount, redCount);
                intent.putExtra(Extra_redCount, redCount);
                intent.putExtra(Extra_virCoin, virCoin);
                setResult(Activity.RESULT_OK,intent);
                Notice.alertAndFinish(context,"已发送~");
            }
        }
        return false;
    }
}
